
// <copyright file="IgnoreAssemblyVersionEqualityComparer.cs" company="Subhadra Software Solutions">
//   
// </copyright>
// <summary>
//   The ignore assembly version equality comparer.
// </summary>

namespace SubhadraSolutions.Sharp.Utils.Reflection.CodeSerialization
{
    using System.Collections.Generic;

    /// <summary>
    /// The ignore assembly version equality comparer.
    /// </summary>
    public sealed class IgnoreAssemblyVersionEqualityComparer : IEqualityComparer<string>
    {
        #region Static Fields

        /// <summary>
        /// The instance.
        /// </summary>
        private static readonly IgnoreAssemblyVersionEqualityComparer InstanceObject = new IgnoreAssemblyVersionEqualityComparer();

        #endregion Static Fields

        #region Constructors and Finalizers

        /// <summary>
        /// Prevents a default instance of the <see cref="IgnoreAssemblyVersionEqualityComparer"/> class from being created.
        /// </summary>
        private IgnoreAssemblyVersionEqualityComparer()
        {
        }

        #endregion Constructors and Finalizers

        #region Public Properties

        /// <summary>
        /// Gets the instance.
        /// </summary>
        public static IgnoreAssemblyVersionEqualityComparer Instance
        {
            get
            {
                return InstanceObject;
            }
        }

        #endregion Public Properties

        #region Public Methods and Operators

        /// <summary>
        /// The equals.
        /// </summary>
        /// <param name="x">
        /// The x.
        /// </param>
        /// <param name="y">
        /// The y.
        /// </param>
        /// <returns>
        /// The <see cref="bool"/>.
        /// </returns>
        public bool Equals(string x, string y)
        {
            if (x == y)
            {
                return true;
            }

            var parserX = new TypeParser(x);
            var parserY = new TypeParser(y);
            if (parserX.ShortAssemblyName == parserY.ShortAssemblyName)
            {
                if (parserX.ShortTypeName == parserY.ShortTypeName)
                {
                    return true;
                }
            }

            return false;
        }

        /// <summary>
        /// The get hash code.
        /// </summary>
        /// <param name="obj">
        /// The obj.
        /// </param>
        /// <returns>
        /// The <see cref="int"/>.
        /// </returns>
        public int GetHashCode(string obj)
        {
            var parser = new TypeParser(obj);
            return (parser.ShortAssemblyName + "\\" + parser.ShortTypeName).GetHashCode();
        }

        #endregion Public Methods and Operators
    }
}
